<?php
/**
 * Created by PhpStorm.
 * User: alisleepy
 * Date: 2019/2/27
 * Time: 10:38
 */
//引入memcache类
require_once('../memcache/memcache.class.php');

//获取access_token
function get_access_token(){
    $test_access_token = Memcacheds::get_cache('test_access_token');
    if($test_access_token){
        //如果缓存存在access_token，直接返回
        return $test_access_token;
    }
    //缓存没有$test_access_token,从微信获取，再存入缓存
    $appid = 'wxd17a3baf67b5eb59';
    $appsecret = '53493ffae58f62fe215cc3925ccb57eb';
    //xb
    // $appid = 'wx5788137027e155d5';
    // $appsecret = '6a3a12f21af7e6770169e2cdb511f482';
    $access_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
    $access_tokenArr = getJson($access_token_url);
    $access_token = $access_tokenArr['access_token'];
    $time = $access_tokenArr['expires_in'];
    Memcacheds::set_cache('test_access_token', $access_token, $time);
    return $access_token;
}

//输出json信息
function echo_Json($code, $msg, $data = array()){
    if($data){
        $result = array('code'=>$code, 'msg'=>$msg, 'data'=>$data);
    }else{
        $result = array('code'=>$code, 'msg'=>$msg);
    }
    echo json_encode($result);
    die;
}

//curl get请求
function getJson($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

//curl post 参数为array()
function postJson($url = '', $post_data = array()) {
    if (empty($url) || empty($post_data)) {
        return false;
    }
    $o = "";
    foreach ( $post_data as $k => $v ) {
        $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);
    $postUrl = $url;
    $curlPost = $post_data;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return json_decode($data, true);
}

//curl post 参数为 data(),请求是可以转化为json字符串
function jsonpostJson($url, $data = NULL, $json = false){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if(!empty($data)) {
        if($json && is_array($data)){
            $data = json_encode( $data,JSON_UNESCAPED_UNICODE );    //注意：json序列化时添加参数JSON_UNESCAPED_UNICODE，否则中文出问题
        }
        //var_dump($data);exit;
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        if($json){ //发送JSON数据
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_HTTPHEADER,
                array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length:' . strlen($data))
            );
        }
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);
    if($errorno) {
        return array('errorno' => false, 'errmsg' => $errorno);
    }
    curl_close($curl);
    return json_decode($res, true);
}

//处理多维数组中的空值
function array_filter_recursive(array &$arr){
    if (empty($arr)) {
        return [];
    }
    foreach ($arr as $k => $v) {
        if (is_array($v)) {
            $arr[$k] = array_filter_recursive($v);
        }
        //$v == false不清除
        if($v === '' || $v === null){
            unset($arr[$k]);
        }
    }
    return $arr;
}